<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="模块类型" prop="homeType">
        <el-select v-model="queryParams.homeType" placeholder="模块类型" clearable>
          <el-option v-for="dict in dict.type.spoken_home_content" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="moduleList">
      <el-table-column label="模块类型" align="center">
        <template slot-scope="scope">
          <span>{{ selectDictLabel(dict.type.spoken_home_content, String(scope.row.homeType)) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <ImagePreview :src="scope.row.homeImage" :width="40"></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="显示排序" width="200"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item label="图片" prop="homeImage">
          <ImageUpload v-model="form.homeImage" :limit="1"></ImageUpload>
        </el-form-item>
        <el-form-item label="内容" prop="homeContent">
          <Editor v-model="form.homeContent" :minHeight="300"></Editor>
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const contentValidate = (rule, value, callback) => {

  if (!value || value == '<p><br></p>') {
    callback(new Error('内容不能为空'))
  }
  else {
    callback()
  }
}

import { listHomeContent, getHomeContent, addHomeContent, updateHomeContent } from '@/api/spoken/home.js'
export default {
  dicts: ['spoken_home_content'],
  data() {
    return {
      form: {},
      // 查询参数
      queryParams: {
        homeType: undefined
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      moduleList: [],
      rules: {
        homeImage: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        homeContent: [
          { required: true, validator: contentValidate, trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listHomeContent(this.queryParams).then(response => {
        this.moduleList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        homeImage: undefined,
        homeContent: undefined,
        homeType: undefined,
        homeId: undefined,
        sort: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getHomeContent() {

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getHomeContent(row.homeId).then(res => {
        this.form = res.data
        this.open = true;
        this.title = "修改";
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 编辑
          if (this.form.homeId) {
            updateHomeContent(this.form).then(res => {
              this.$modal.msgSuccess("操作成功");
              this.open = false
              this.getList();
            })
          } else {  // 新建
            this.form.homeType = this.type
            addHomeContent(this.form).then(res => {
              this.getHomeContent()
              this.$modal.msgSuccess("操作成功");
              this.open = false
              this.getList();
            })
          }
        }
      });
    },
  }
}
</script>
<style lang="scss" scoped></style>